define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'anchor/index',
                    add_url: 'anchor/setLevel',
                    // edit_url: 'anchor/edit',
                    // del_url: 'anchor/del',
                    // multi_url: 'anchor/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'username', title: __('Host ID')},
                        {field: 'nickname', title: __('Nickname'), operate: false},
                        {field: 'zone_list.zonename', title: __('District'), searchList: Config.zoneList},
                        {
                            field: 'user_right_config.gradeName',
                            title: __('VIP Level'),
                            searchList: Config.grade,
                        },
                        {field: 'fansNum', title: __('Fans'), operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: false},
                        {
                            field: 'signature',
                            title: __('Personal Statement'),
                            operate: false,
                            formatter : function(value, row, index, field){
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            // 固定列最大宽度，超出隐藏
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {field: 'twitter_id', title: __('Twitter_id'), operate: false},
                        {field: 'youtube_channel_id', title: __('Youtube_channel_id'), operate: false},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: 'RANGE',
                        },
                        {
                            field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {
                                "0": "不在线",
                                "1": "在线"
                            }
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'boxInfo',
                                    text: __('boxInfo'),
                                    title: __('boxInfo'),
                                    classname: 'btn btn-xs btn-success btn-dialog btn-boxinfo boxinfo',
                                    refresh: true,
                                    url: "anchor/boxinfo?robotUser={username}"
                                },
                                {
                                    name: 'setLevel',
                                    text: __('setLevel'),
                                    title: __('setLevel'),
                                    classname: 'btn btn-xs btn-success btn-dialog btn-setLevel setlevel',
                                    refresh: true,
                                    url: "anchor/setLevel?id={username}"
                                },
                            ]
                        }
                    ]
                ]
            });

            $(document).on('click', '.btn-setting', function () {
                var ids = Table.api.selectedids(table);
                var url = 'anchor/setting';
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
            });

            $(document).on('click', '.btn-hot_setting', function () {
                var ids = Table.api.selectedids(table);
                var url = 'anchor/hot_setting';
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-searchUser").on("click", function () {
                let id = $("#c-livestreamerUser").val();
                if (id == '' || id == null) {
                    Backend.api.toastr.error('请输入主播id');
                    return
                }
                Fast.api.ajax({
                    url: 'anchor/searchUser?id=' + id,
                    loading: true
                }, function (data) {
                    console.log(data.level)
                    $("#c-addNickname").val(data.nickname)
                    $("#zone").selectpicker("val", data.zone)
                    $("#grade").selectpicker("val", data.level)
                }, function (data, ret) {
                    Layer.msg(ret.msg)
                    return false
                })
            })

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        setting: function () {
            $('.zoneSelect').on('change', function () {
                $("#forGirlOne").text('');
                $("#forGirlTwo").text('');
                $("#forBoyOne").text('');
                $("#forBoyTwo").text('');
                if ($(this).val() != null && $(this).val() != 0) {
                    Fast.api.ajax({
                        url: "anchor/getSetting?id=" + $(this).val(),
                        loading: true
                    }, function (data) {
                        $("#forGirlOne").text(data.forGirlOne);
                        $("#forGirlTwo").text(data.forGirlTwo);
                        $("#forBoyOne").text(data.forBoyOne);
                        $("#forBoyTwo").text(data.forBoyTwo);
                    }, function (data, ret) {
                        Layer.msg(ret.msg)
                        return false
                    })
                }
            });

            Controller.api.bindevent()
        },
        hot_setting: function () {
            Controller.api.bindevent()
        },
        boxinfo: function (id) {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'anchor/boxinfo' + location.search,
                    // add_url: 'anchor/add',
                    // edit_url: 'anchor/edit',
                    // del_url: 'anchor/del',
                    // multi_url: 'anchor/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'recordTime',
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                renderDefault: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'robotUser', title: __('robotUser'), operate: 'like', },
                        {
                            field: 'recordTime',
                            title: __('recordTime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                        },
                        {field: 'getDiamonds', title: __('getDiamonds'), operate: false},
                    ]
                ]
            });

            Controller.api.bindevent()
        },
        setlevel: function () {

            $(".btn-searchUser").on("click", function () {
                let id = $("#c-Id").val();
                if (id == '' || id == null) {
                    Backend.api.toastr.error('请输入主播id');
                    return
                }
                Fast.api.ajax({
                    url: 'anchor/searchUser?id=' + id,
                    loading: true
                }, function (data) {
                    $("#c-userName").val(data.username)
                    $("#c-grade").val(data.level)
                    $("select[name=zone]").selectpicker('val', data.zone)
                    console.log($("select[name=zone]").val())
                }, function (data, ret) {
                    Layer.msg(ret.msg)
                    return false
                })
            })

            Controller.api.bindevent()
        },
    };
    return Controller;
});
